<?php

namespace app\adminmall\controller;
use app\adminmall\model\PaylogModel;
use app\common\model\Member;
use app\common\model\Csv;
use app\adminmall\model\CashmanageModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 交易记录
 */
class PaylogController extends AdminBaseController
{
 	 protected $model;
	 protected $csv;
	
    protected function _initialize() {
        parent::_initialize();
		$this->csv = new Csv();
        $this->model = new PaylogModel();
    }
	/**
	 * 商家收款记录
	 */
	public function sk_record(){
	 	$param = input('post.');
		$query=[];
		//商家
		if(input('name')){
			$query['name'] = input('name');
			$this->assign("name",input("name"));
			$map['g.store_name'] = ['like', '%'.input('name').'%'];
		}else{
			$this->assign("name",'');
		}
		//添加时间
		if(input('add_time')&&input('end_time')){
			$map['m.add_time'] = ['between time', [input('add_time'),input('end_time')]];
		}
		$map['m.type'] = 'ads_order';
		$field = 'm.*,g.store_name,g.store_tel,b.nick_name,b.mobile';
		$list=db("mall_corepaylog")->alias('m')
		->join('mall_agent g','g.id = m.tid')
		->join('member b','b.id = m.uid')
		->where($map)->field($field)->order('m.add_time desc')->paginate(20,false,['query'=>$query]); 
		$this->assign("list",$list);
		return $this->fetch();
	}
	
	/**
	 * 商家收款记录导出
	 */
	public function sk_recorddc(){
	 	$param = input('post.');
		$query=[];
		//商家
		if(input('name')){
			$query['name'] = input('name');
			$this->assign("name",input("name"));
			$map['g.store_name'] = ['like', '%'.input('name').'%'];
		}else{
			$this->assign("name",'');
		}
		//添加时间
		if(input('add_time')&&input('end_time')){
			$map['m.add_time'] = ['between time', [input('add_time'),input('end_time')]];
		}
		$map['m.type'] = 'ads_order';
		$field = 'm.*,g.store_name,g.store_tel,b.nick_name,b.mobile';
		$list=db("mall_corepaylog")->alias('m')
		->join('mall_agent g','g.id = m.tid')
		->join('member b','b.id = m.uid')
		->where($map)->field($field)->order('m.add_time desc')->select(); 
		
		$str ="收款商家名称,收款商家电话,付款人姓名,付款人电话,金额,支付方式,时间,状态,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$store_name = iconv('utf-8','gbk',$row['store_name']);
			$store_tel = iconv('utf-8','gbk',$row['store_tel']);
			$nick_name = utf8ToGbk($row['nick_name']);
			$mobile = iconv('utf-8','gbk','	'.$row['mobile']);
			$price = iconv('utf-8','gbk',$row['price']);
			if($row['pay_type']==1){
				$payname = '余额';
			}else if($row['pay_type']==2){
				$payname = '微信';
			}else if($row['pay_type']==3){
				$payname = '支付宝';
			}else {
				$payname = '银联';
			}
			$typename = iconv('utf-8','gbk',$payname); 
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);
			if($row['status']==2){
				$statusname = '成功';
			}else {
				$statusname = '失败';
			}
			$statusname = iconv('utf-8','gbk',$statusname); 
			$str .= $store_name.",".$store_tel.",".$nick_name.",".$mobile.",".$price.",".$typename.",".$add_time.",".$statusname.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家收款记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	/**
	 * 会员收款记录
	 */
	public function memsk_record(){
		if(request()->isPost()){
		 	$param = input('post.');
			//会员
			if($param['name']){
				$map['g.nick_name'] = ['like', '%'.$param['name'].'%'];
			}
			//添加时间
			if($param['add_time']&&$param['end_time']){
				$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}
	    }
		$map['m.type'] = 'uesr_payee';
		$field = 'm.*,g.nick_name as sname,g.mobile as smobile ,b.nick_name,b.mobile';
		$list=db("mall_corepaylog")->alias('m')
		->join('member g','g.id = m.tid')
		->join('member b','b.id = m.uid')
		->where($map)->field($field)->order('m.add_time desc')->paginate(20); 
		return $this->fetch('memsk_record', ['list' => $list]);
	}
	
	
	/**
     * 商家余额记录
     * @author [lx] 
     */
    public function balancerec(){
    	$map = [];
		$query=[];
    	$field = 'm.*,g.store_name';
	 	$param = input('post.');
		//商家名称
		if(input('name')){
			$query['name']=input('name');
			$this->assign("name",input("name"));
			$map['g.store_name'] = ['like', '%'.input('name').'%'];
		}else{
			$this->assign("name","");
		}
		//添加时间
		if(input('add_time')&&input('end_time')){
			$map['m.add_time'] = ['between time', [input('add_time'),input('end_time')]];
		}
		$map['m.from'] = 1;	
		$list = Db::name("mall_charge")->alias('m')
		->join('mall_agent g','g.id = m.agent_id')->field($field)
		->where($map)->order("m.add_time desc")->paginate(20,false,['query'=>$query]);       

        return $this->fetch('balancerec', ['list' => $list]);
    }

	/**
	 * 商家余额记录导出
	 */
 	public function balancedc(){
    	$map = [];
    	$field = 'm.*,g.store_name';
		$param = input();
		//商家名称
		if($param['name']){
			$map['g.store_name'] = ['like', '%'.$param['name'].'%'];
		}
		//添加时间
		if($param['add_time']&&$param['end_time']){
			$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
		}

		$map['m.from'] = 1;
		$list = Db::name("mall_charge")->alias('m')
		->join('mall_agent g','g.id = m.agent_id')->field($field)
		->where($map)->order("m.add_time desc")->select();   
		
		$str ="商家名称,操作类型,信息,变化金额,账户余额,操作时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$store_name = iconv('utf-8','gbk',$row['store_name']);
			$type = get_mall_charge_type($row['type']); 						
			$typename = iconv('utf-8','gbk',$type); 	
			$message = iconv('utf-8','gbk',$row['message']);
			$balance = iconv('utf-8','gbk',str_replace(',','',$row['balance']));
			$store_balance = iconv('utf-8','gbk',str_replace(',','',$row['store_balance']));
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);	
			$str .= $store_name.",".$typename.",".$message.",".$balance.",".$store_balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家余额记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
    }
	
	/**
     * 会员余额记录
     * @author [lx] 
     */
    public function membalancerec(){
    	$map = [];
    	$field = 'm.*,g.nick_name';
		if(input("uid")){
			$map['m.uid']=input("uid");
		}
		if(input("is_pt")){//判断是否是合伙人
			$map['m.is_pt']=input("is_pt");
		}
		if(request()->isPost()){
		 	$param = input('post.');
			//会员名称
			if($param['name']){
				$map['g.nick_name'] = ['like', '%'.$param['name'].'%'];
			}
			//添加时间
			if($param['add_time']&&$param['end_time']){
				$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}
	    }	
		$map['m.credittype'] = 'credit2';
		$list = Db::name("mall_credits")->alias('m')
		->join('member g','g.id = m.uid')->field($field)
		->where($map)->order("m.add_time desc")->paginate(20);       
        return $this->fetch('membalancerec', ['list' => $list]);
    }

	/**
	 * 会员余额记录导出
	 */
 	public function membalancedc(){
    	$map = [];
    	$field = 'm.*,g.nick_name';
	 	$param = input();
		//会员名称
		if($param['name']){
			$map['g.nick_name'] = ['like', '%'.$param['name'].'%'];
		}
		//添加时间
		if($param['add_time']&&$param['end_time']){
			$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
		}
		$map['m.credittype'] = 'credit2';
		$list = Db::name("mall_credits")->alias('m')
		->join('member g','g.id = m.uid')->field($field)
		->where($map)->order("m.add_time desc")->select();    
		
		$str ="会员名称,操作类型,信息,变化金额,操作时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$nick_name = utf8ToGbk($row['nick_name']);
			$type = get_mall_credits_type($row['type']); 						
			$typename = iconv('utf-8','gbk',$type); 	
			$message = iconv('utf-8','gbk',$row['remark']);
			$price = iconv('utf-8','gbk',$row['price']);
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);	
			$str .= $nick_name.",".$typename.",".$message.",".$price.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员余额记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
    }

	/**
     * 商家积分记录
     * @author [lx] 
     */
   	public function scorerec(){
    	$map = [];
		$field = 'm.*,g.store_name,g.store_tel';		    	
    	if(request()->isPost()){
		 	$param = input('post.');		
			//商家名称
			if($param['name']){
				$map['g.store_name'] = ['like', '%'.$param['name'].'%'];
			}				
			//创建时间
			if($param['add_time']&&$param['end_time']){
				$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}		
	 	}
		$list = Db::name("mall_score_charge")->alias('m')
		->join('mall_agent g','g.id = m.agent_id')->field($field)
		->where($map)->order("m.add_time desc")->paginate(10);   
        return $this->fetch('scorerec', ['list' => $list]);
    }
    
	/**
	 * 商家积分记录导出
	 */
 	public function socredc(){
    	$map = [];
		$field = 'm.*,g.store_name,g.store_tel';		    	
	 	$param = input();		
		//商家名称
		if($param['name']){
			$map['g.store_name'] = ['like', '%'.$param['name'].'%'];
		}				
		//创建时间
		if($param['add_time']&&$param['end_time']){
			$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
		}		
		$list = Db::name("mall_score_charge")->alias('m')
		->join('mall_agent g','g.id = m.agent_id')->field($field)
		->where($map)->order("m.add_time desc")->select();  
		
		$str ="商家名称,商家电话,操作类型,信息,积分,操作时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$store_name = utf8ToGbk($row['store_name']);
			$store_tel = iconv('utf-8','gbk','	'.$row['store_tel']);
			$type = get_mall_score_charge_type($row['type']); 						
			$typename = iconv('utf-8','gbk',$type); 	
			$message = iconv('utf-8','gbk',$row['message']);
			$score = iconv('utf-8','gbk',$row['score']); 
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);	
			$str .= $store_name.",".$store_tel.",".$typename.",".$message.",".$score.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家积分记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
    }
   
   	/**
     * 会员积分记录
     * @author [lx] 
     */
   	public function memscorerec(){
    	$map = [];
		$field = 'm.*,g.nick_name,g.mobile';
		if(input("uid")){
			$map['g.id']=input("uid");
		}	    	
    	if(request()->isPost()){
		 	$param = input('post.');		
			//会员名称
			if($param['name']){
				$map['g.nick_name'] = ['like', '%'.$param['name'].'%'];
			}				
			//创建时间
			if($param['add_time']&&$param['end_time']){
				$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}		
	 	}
		$map['m.credittype'] = 'credit1';
		$list = Db::name("mall_credits")->alias('m')
		->join('member g','g.id = m.uid')->field($field)
		->where($map)->order("m.add_time desc")->paginate(20);   
        return $this->fetch('memscorerec', ['list' => $list]);
    }

	/**
	 * 会员积分记录导出
	 */
 	public function memsocredc(){
    	$map = [];
		$field = 'm.*,g.nick_name,g.mobile';		    	
	 	$param = input();		
		//会员名称
		if($param['name']){
			$map['g.nick_name'] = ['like', '%'.$param['name'].'%'];
		}				
		//创建时间
		if($param['add_time']&&$param['end_time']){
			$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
		}		
		$map['m.credittype'] = 'credit1';
		$list = Db::name("mall_credits")->alias('m')
		->join('member g','g.id = m.uid')->field($field)
		->where($map)->order("m.add_time desc")->select();
		
		$str ="会员名称,会员电话,操作类型,信息,积分,操作时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$store_name = utf8ToGbk($row['nick_name']);
			$mobile = iconv('utf-8','gbk','	'.$row['mobile']);
			$type = get_mall_credits_type($row['type']); 						
			$typename = iconv('utf-8','gbk',$type); 	
			$message = iconv('utf-8','gbk',$row['remark']);
			$score = iconv('utf-8','gbk',$row['price']); 
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);	
			$str .= $store_name.",".$mobile.",".$typename.",".$message.",".$score.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员积分记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
     }




	/**
	 * 获取商家信息
	 */
	public function get_agent(){
		$agent=db('mall_agent')->find(input("agent_id"));
		return json(['agent'=>$agent]);
	}
	/**
	 * 获取商家信息
	 */
	public function get_member(){
		$member=db('member')->find(input("member_id"));
		return json(['member'=>$member]);
	}






	
    /**
     * 交易记录列表
     * @author [lx] 
     */
    public function index( $page = 1){
    	$list=$this->model
    	->table('long_member m,long_mall_corepaylog c')
    	->where('c.uid=m.id and c.status=2')
    	->paginate(10); 

        
        return $this->fetch('index', ['list' => $list]);
    }
    /**
     * [search查询]
     * @author [lx] 
     */
    public function search(){
    	$map = [];
    	$field = 'm.*,g.id,g.mobile,g.nick_name,g.city';
		if(request()->isPost()){
		 	$param = input('post.');
		 	//用户类型
			if($param['user_type'] != 0){
				$map['user_type'] = $param['user_type'];
			}
			
			//用户名称
			if($param['name']){
				$map['nick_name'] = ['like', '%'.$param['name'].'%'];
			}
			//添加时间
			if($param['add_time']&&$param['end_time']){
				$map['m.add_time'] = ['between time', [$param['add_time'],$param['end_time']]];
			}
	    }	
    		$list=Db::name("mall_corepaylog")
    		->alias('m')
    		->join('__MEMBER__ g','g.id=m.uid')
    		->field($field)
    		->where($map)
    		->order("m.add_time desc")
    		->paginate(10);       
        return $this->fetch('index', ['list' => $list]);
    }
}
